﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using automobili_data;
using System.Net.Mail;
using automobili_data.interfaces;

namespace automobili_gui.UserManagement
{
    public partial class Registracija : System.Web.UI.Page
    {
        User current = null;
        Privilegije p = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            p = SessionUtils.getCurrentPrivileges();
            if (p == null) { p = AutomobiliDataManager.instance.dohvatiPodrazPrivilegije(); }
            if (p.Registracije_s == 1)
            {
                duzIme.Text = "";
                duzPrezime.Text = "";
                lozDuzina.Text = "";
                ponLozDuzina.Text = "";
            }
        }

        private void SendEMail (MailMessage mailMessage)
        {
            SmtpClient client = new SmtpClient();
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential("automobil.portal@gmail.com", "iep_2013");
            client.Host = "smtp.gmail.com";

            try
            {
                client.Send(mailMessage);
            }
            catch (SmtpException)
            {
                throw;
            }
        }

        public void Send ()
        {
            MailMessage Message = new MailMessage();
            MailAddress Email = new MailAddress("jeca_dk@yahoo.com");
         //   MailAddress Email = new MailAddress(email.Text);
            Message.To.Add(Email);
            Message.From = new MailAddress("automobil.portal@gmail.com");
            Message.Subject = "Proba";
            Message.Body = "Registrovani ste na Automobili Portal. Vase korisnicko ime je: " + ime.Text;
            SendEMail(Message);
        }
        protected void registrationForm_Authenticate(object sender, EventArgs e)
        {
            int neispravno = 0;
            //ne dohvata vrednosti, reaguje samo kada je prazno...
            if (ime.Text.Length < 2)
            {
                if (ime.Text.Length == 0) { duzIme.Text = "Morate uneti ime!"; }
                else {
               duzIme.Text = "Ime mora da ima najmanje 2 slova!";
                neispravno++; }
            }

            if (prezime.Text.Length < 2)
            {
                if (prezime.Text.Length == 0) { duzPrezime.Text = "Morate uneti prezime"; }
                else
                {
                    duzPrezime.Text = "Prezime mora da ima najmanje 2 slova!";
                    neispravno++;
                }
            }

            if (lozinka.Text.Length < 8)
            {
                if (lozinka.Text.Length == 0) { lozDuzina.Text = "Morate uneti lozinku!"; }
                else
                {
                    lozDuzina.Text = "Lozinka mora imati minimum 8 karaktera!";
                    neispravno++;
                }
            }

            if (ponoviLozinka.Text.Length == 0) {
                ponLozDuzina.Text = "Morate ponoviti lozinku!";
                neispravno++;
            }

            if (neispravno == 0)
            {
                current = automobili_data.AutomobiliDataManager.instance.getUserN(email.Text);
                //  try
                // {
                if (current == null)
                {
                    automobili_data.AutomobiliDataManager.instance.createUser(ime.Text, prezime.Text, email.Text, lozinka.Text);
                    automobili_data.AutomobiliDataManager.instance.kreirajProfil(email.Text);
                    this.Send();
                    Response.Redirect(WebSiteController.onSuccessfullyRegisterUrl(), true);
                }
                else
                // catch (automobili_data.interfaces.exceptions.UserException ue)
                {

                    reg.Text = "Vec postoji korisnik";

                }
            }
        }
    }
}